<template>
  <div class="carbon-credit-detail">
    <!-- 返回按钮 -->
    <button class="back-btn" @click="goBack">
      <i class="el-icon-arrow-left"></i> 返回
    </button>
    
    <!-- 标题 -->
    <div class="page-title">碳信用详情</div>


    <div class="block">
      <div class="block-header">
        <i class="el-icon-menu"></i> 基本信息
      </div>
      <!-- 用 Grid 布局实现“一行多列” -->
      <div class="info-grid">
        <div class="info-row">
          <div class="info-item">
            <label>核证标准</label>
            <span>{{ tableData.certificationCriteria || '--' }}</span>
          </div>
          <div class="info-item">
            <label>采用方法学</label>
            <span>{{ tableData.carbonMethodologyName || '--' }}</span>
          </div>
          <div class="info-item">
            <label>类型</label>
            <span>{{ tableData.projectScopeType || '--' }}</span>
          </div>
          <div class="info-item">
            <label>核证机构</label>
            <span>{{ tableData.certifiedAgency || '--' }}</span>
          </div>
       
          <div class="info-item">
            <label>签发日期</label>
            <span >{{ tableData.issuingDate || '--' }}</span>
          </div>
        </div>
        <div class="info-row">
         
          <div class="info-item">
            <label>签发凭证</label>
            <span style="width: 160px; color: #126fff; cursor: pointer;
        display: inline-block; max-width: 600px;white-space:nowrap;
        overflow: hidden;text-overflow: ellipsis"  >
              <a  @click="handleView(tableData.issuingCertificates)"  target="_blank">{{tableData.issuingCertificates || '--'}}</a>
            </span>
          </div>
          <div class="info-item" style="width: 220px;">
            <label>一级市场持有机构</label>
            <span>{{tableData.projectOwner}}</span>
          </div>
        </div>
      </div>
    </div>

    <div class="block">
      <div class="block-header">
      </div>
      <div class="project-info">
        <span></span>
      </div>
    </div>

    <!-- 持仓信息区块 -->
    <div class="block">
      <div class="block-header">
        <i class="el-icon-s-data"></i> 
        持仓信息 
      </div>
      <div class="info-grid">
        <div class="info-row">
          <div class="info-item">
            <label>资产状态</label>
            <span>{{ tableData.projectStatus }}</span>
          </div>
          <div class="info-item">
            <label>持仓总量(tCO₂e)</label>
            <span>{{tableData.total}}</span>
          </div>
          <div class="info-item">
            <label>资产估值(¥)</label>
            <span>{{ tableData.valuation }}</span>
          </div>
          <div class="info-item">
            <label>可用数量(tCO₂e)</label>
            <span>{{tableData.availableAmount}}</span>
          </div>
          <div class="info-item">
            <label>锁定数量(tCO₂e)</label>
            <span>{{ tableData.lockedAmount }}</span>
          </div>
          <div class="info-item">
            <label>冻结数量(tCO₂e)</label>
            <span>{{tableData.frozenAmount}}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CarbonCreditDetail',
  data(){
    let Id = this.$route.query.id
      return{
        id:Id,
        user : JSON.parse(localStorage.getItem('user') || '{}'),
        tableData:[]
      }
  },

mounted(){
      this.queryCreditSum();
      this.queryCredits();
  },

   methods: {
    // 返回上一级页面方法
    goBack() {
      this.$router.go(-1);
    },

    queryCredits(){

        this.$request.get('/assets/carbonCreditAssets/queryCreditAssets',
        { params: {
           tenantId: this.user.tenantId,
           id:this.id
         }}
        ).then(res =>{
          console.log("这是全部碳信用数据",res)
          if(res.code === 200){  
            this.tableData = res.data
          }

        })
    },
    handleView(row) {
        const button = document.activeElement;
      // 构建百度搜索URL，将交易所名称作为搜索关键词
      const searchUrl = `${row}`;
      
      // 在新窗口打开百度搜索页面
      window.open(searchUrl, '_blank');
      if (button && button.blur) {
      // 延迟执行，确保跳转逻辑完成后再失焦
       setTimeout(() => {
      button.blur();
   
    }, 100);
      }
 },

    queryCreditSum(){
        this.$request.get('/assets/carbonCreditAssets/queryCreditAssetsSum',
        { params: {
           tenantId: this.user.tenantId
         }}
        ).then(res =>{
          if(res.code === 200){  
            this.holdTotal = res.data.total
            this.locked = res. data.lockedAmount
            this.frozen = res.data.frozenAmount
            this.available = res.data.availableAmount
          }

        })
    }

  }
}
</script>

<style scoped>
.carbon-credit-detail {
  padding: 24px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

/* 返回按钮样式 */
.back-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  background-color: #f5f5f5;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  cursor: pointer;
  color: #333;
  font-size: 14px;
  transition: all 0.2s;
  margin-bottom: 12px; /* 与标题保持间距 */
}

.back-btn:hover {
  background-color: #e9e9e9;
  border-color: #d0d0d0;
}

.back-btn i {
  color: #2dca73;
}

/* 页面标题 */
.page-title {
  font-size: 16px;
  font-weight: bold;
  color: #2dca73;
  margin-bottom: 20px;
}

/* 区块通用样式 */
.block {
  margin-bottom: 32px;
}

/* 区块头部（带图标） */
.block-header {
  display: flex;
  align-items: center;
  font-size: 16px;
  font-weight: bold;
  color: #333;
  margin-bottom: 16px;
}

.block-header i {
  color: #2dca73;
  margin-right: 8px;
}

/* 查看详情链接 */
.view-detail {
  margin-left: auto;
  color: #2dca73;
  text-decoration: underline;
  cursor: pointer;
  font-weight: normal;
}

/* 网格布局容器 */
.info-grid {
  display: flex;
  flex-direction: column;
  gap: 100px;
}

/* 每一行（横向排列多个 info-item） */
.info-row {
  display: flex;
  flex-wrap: wrap;
  gap: 22px;
}

/* 每个信息项（标题 + 内容） */
.info-item {
  width: 180px;
  display: flex;
  flex-direction: column;
}

.info-item label {
  color: #666;
  font-weight: 800;
  font-size: 15px;
  margin-bottom: 4px;
}

.info-item span {
  margin-top: 6px;
  color: #333;
  font-size: 14px;
}

/* 项目信息特殊样式 */
.project-info {
  background: #f9f9f9;
  padding: 20px;
  width: 100%;
  border-radius: 4px;
  color: #676565;
  line-height: 1.6;
}
</style>